
var layer = layui.layer //弹层
    ,table = layui.table //表格
    ,form = layui.form,$ = layui.jquery,laydate = layui.laydate;
layui.use(['tableEdit',"excel"], function () {
    let tableEdit = layui.tableEdit;
    let layer = layui.layer,
        excel = layui.excel;

    lay('.starttime').each(function () {
        laydate.render({
            elem: this
        });
    });

    function getAllJson(jsons) {
        let tmpitem = []
        for (let item of jsons) {
            let tmparr = {}
            tmparr["value"] = item["code"];
            tmparr["name"] = item["name"];
            if (item["users"]) {
                for (let it of item["users"]) {
                    item["list"].push(it)
                }
            }
            if (item["list"] && item["list"].length) {
                tmparr["children"] = getAllJson(item["list"]);
                tmpitem.push(tmparr)
            } else {
                tmpitem.push(tmparr)
            }

        }
        return tmpitem
    }

    function getxmselect(el, name, data) {
        return xmSelect.render({
            el: el,
            name: name,
            tree: {
                show: true,
                showLine: true,
                showFolderIcon: true,
                indent: 20,
                expandedKeys: false
            },
            height: 'auto',
            data: data,
            on: function ({arr, change, isAdd}) {
                return this
            }
        })
    }

    let billNoStatusList=[];
    let operationList=[];
    let deptOperationList=[];
    layer.ready(function () {
        //获取所有公司信息
        $.ajax({
            url: "receivePayment/getCompanyInfo",
            success: function (data) {
                if (data.status == 200) {
                    for (let i in data.data) {
                        //公司名称
                        $('#signingCompanyCode').append(new Option(data.data[i].name, data.data[i].code));
                    }
                    form.render('select');
                }
            }
        });

        //获取当前用户的部门及人员结构树
        let treeList = getCurrentDepartmentUser();

        if (treeList && treeList.list) {
            let userTree = getAllJson(treeList.list);
            operationList=getxmselect("#operationCode", "operationCode", userTree);
        } else {
            operationList=getxmselect("#operationCode", "operationCode", [{name: user.name, value: user.code}]);
        }
        //获取当前用户所属部门树()
        let currentDeptTreeList = getAllUserDepartmentTree();
        if (currentDeptTreeList && currentDeptTreeList.list) {
            let deptTree = getAllJson(currentDeptTreeList.list);
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", deptTree);
        } else {
            deptOperationList = getxmselect("#deptOperationCode", "deptOperationCode", [{name: user.name, value: user.code}]);
        }

        //单据信息
        let billNoStatus = [
            {name: "草稿箱", value: "1"},
            {name: "待批箱", value: "2"},
            {name: "已批箱", value: "3"},
            {name: "完成箱", value: "4"},
            {name: "垃圾箱", value: "5"}
        ];
        billNoStatusList=getxmselect("#status", "status", billNoStatus);

    });
    form.render();//表单刷新

    let initData = [];
    table.render({
        elem: '#offerselect'
        , data: initData
        , where: {operationCode: user.code}
        , url: 'offer/statistics/getOfferProductStatisticsByPage'
        , toolbar: '#offertoolbar'
        ,totalRow: true
        , cols: [[
            {field: 'numbers',width: 50,type:'numbers',title:'序号', totalRowText: '合计'}
            ,{field: 'billNo', title: '报价单号', sort: true, width: 240, event: "quotationno"}
            , {field: 'time', title: '报价日期', sort: true, width: 120}
            , {field: 'customerName', title: '客户名称', sort: true, width: 240}
            , {field: 'termOfValidity', title: '报价有效期限', sort: true, width: 120}
            , {field: 'deliveryPeriod', title: '交货期限', sort: true, width: 120}
            , {field: 'priceTermsName', title: '价格术语', sort: true, width: 240}
            , {field: 'salesman', title: '业务员', sort: true, width: 120}
            , {field: 'operation', title: '操作员', sort: true, width: 120}
            , {field: 'currency', title: '币种', sort: true, width: 120}
            , {field: 'productCode', title: '产品编码', sort: true, width: 120}
            , {field: 'specificationCode', title: '规格编码', sort: true, width: 120}
            , {field: 'productCname', title: '中文名称', sort: true, width: 120}
            , {field: 'productEname', title: '英文名称', sort: true, width: 120}
            , {field: 'unitPrice', title: '单价', sort: true, width: 120}
            , {field: 'productNum', title: '数量', sort: true, width: 120, totalRow: true}
            , {field: 'moneySubtotalUSD', title: 'USD金额小计', sort: true, width: 120, totalRow: true}
            , {field: 'moneySubtotalRMB', title: 'RMB金额小计', sort: true, width: 120, totalRow: true}
            , {field: 'isDeal', title: '是否成交', sort: true, width: 120}
            , {field: 'signingCompanyName', title: '签约公司', sort: true, width: 250}
            , {field: 'memo', title: '备注', width: 220}
        ]]
        , id: 'offertableid'
        , height: $(document).height() - $('#offerselect').offset().top - 50
        , page: true
        , limit: 300
        , limits: [300, 600, 900]
    });

    table.on('tool(offertable)', function(obj){
        let data = obj.data;
        let id = data.id;
        if (obj.event === "quotationno"){
            parent.intab("views/jsp/ExportBusiness/tpl/viewoffer.jsp?id="+id,"报价明细")
        }
    });

    form.on('submit(submitform)', function (data) {
        table.reload('offertableid', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: data.field
        }, 'data');
        return false;
    });

    form.on('submit(exportData)', function (queryReq) {
        let index = layer.load(2, {
            shade: [0.4, '#000'],
            content: '加载中...',
            success: function (layero) {
                layero.find('.layui-layer-content').css({
                    'padding-top': '40px',
                    'width': '200px'
                });
            }
        });
        $.ajax({
            data: queryReq.field,
            url:"offer/statistics/export/getOfferProductStatisticsInfo",
            success: function(res) {
                if(res.status == 200){
                    //配置列宽
                    let colConf = excel.makeColConfig({
                        'A': 80,
                        'X': 80
                    }, 80);
                    excel.exportExcel({
                        sheet1: res.data.data
                    }, '报价产品详情列表.xlsx', 'xlsx', {
                        extend: {
                            '!cols': colConf
                        }
                    });
                }else {
                    layer.msg(res.msg, {icon: 5});
                }
                layer.close(index);
                return false;
            }
        });
        return false;
    });
    document.getElementById('clear').onclick = function(){
        operationList.setValue([ ]);
        billNoStatusList.setValue([ ]);
        deptOperationList.setValue([ ]);
    };
});